<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

    <!--引入host文件中配置的域名   这是一个自定义的文件-->
    <script src="../js/pages/public.js"></script>

</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->
<div class="box-header with-border">
    <h3 class="box-title">规格管理</h3>
</div>
<div class="box-body">
    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#InserteditModal"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" onclick="DelteSpec()" title="删除"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>

                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <br>
        <hr>
        <form id="FormId">
            <input style="width: 200px" name="specName" class="form-control" placeholder="规格名称">
        </form>
        <button type="button" class="btn btn-default" title="搜索" onclick="findSpecList()"><i class="fa fa-refresh"></i>
            搜索
        </button>
        <!--工具栏/-->
        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">规格ID</th>
                <th class="sorting">规格名称</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="HtmlId">
            </tbody>
        </table>
        <!--数据列表/-->
    </div>
    <!-- 数据表格 /-->
    <form id="indexForm">
        <a href="javascript:toPage(3)">首页</a>
        <a href="javascript:toPage(1)">上一页</a>
        <a href="javascript:toPage(2)">下一页</a>
        <a href="javascript:toPage(4)">尾页</a>
        第<input type="text" name="pageNum" id="pageNum" size="1px" readonly="readonly">页
        共<input type="text" name="pages" id="pages" size="1px" disabled="disabled">页
        共<input type="text" name="total" id="total" size="1px" disabled="disabled">条
    </form>
</div>
<!-- /.box-body -->


<!-- 编辑窗口 -->
<div class="modal fade" id="InserteditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">规格编辑</h3>
            </div>
            <div class="modal-body">

                <form id="InsertForm">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>规格名称</td>
                            <td><input name="InsertspecName" class="form-control" placeholder="规格名称"></td>
                        </tr>
                    </table>
                </form>

                <!-- 规格选项 -->
                <div class="btn-group">
                    <button id="addspecOption" type="button" class="btn btn-default" title="新建"><i
                            class="fa fa-file-o"></i> 新增规格选项
                    </button>

                </div>

                <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                    <tr>
                        <th class="sorting">规格选项</th>
                        <th class="sorting">排序</th>
                        <th class="sorting">操作</th>
                    </thead>
                    <tbody id="spec_add_tbody">
                    <!--                    <tr>-->
                    <!--                        <td>-->
                    <!--                            <input class="form-control" placeholder="规格选项">-->
                    <!--                        </td>-->
                    <!--                        <td>-->
                    <!--                            <input class="form-control" placeholder="排序">-->
                    <!--                        </td>-->
                    <!--                        <td>-->
                    <!--                            <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除-->
                    <!--                            </button>-->
                    <!--                        </td>-->
                    <!--                    </tr>-->
                    </tbody>
                </table>


            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="InsertSpec()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>


<!--修改窗口-->
<div class="modal fade" id="UpdateeditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel1">规格编辑</h3>
            </div>
            <div class="modal-body">

                <form id="specId">
                    <input type="hidden" id="id1" name="id" value="id">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>规格名称</td>
                            <td><input id="specName" name="specName" class="form-control" placeholder="规格名称"></td>
                        </tr>
                    </table>
                </form>

                <!-- 规格选项 -->
                <div class="btn-group">
                    <button id="updatespecOption" type="button" class="btn btn-default" title="新建"><i
                            class="fa fa-file-o"></i> 新增规格选项
                    </button>
                </div>
                <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                    <tr>
                        <th class="sorting">规格选项</th>
                        <th class="sorting">排序</th>
                        <th class="sorting">操作</th>
                    </thead>
                    <tbody id="spec_update_tbody">
                    <!--                    <tr>-->
                    <!--                        <td>-->
                    <!--                            <input class="form-control" placeholder="规格选项">-->
                    <!--                        </td>-->
                    <!--                        <td>-->
                    <!--                            <input class="form-control" placeholder="排序">-->
                    <!--                        </td>-->
                    <!--                        <td>-->
                    <!--                            <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除-->
                    <!--                            </button>-->
                    <!--                        </td>-->
                    <!--                    </tr>-->
                    </tbody>
                </table>


            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="Update()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript">
    $(function () {
        getSpecList()
    })

    function getSpecList() {
        $.ajax({
            url: domain + "/spec/getSpecList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            // contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: $("#indexForm").serialize(),//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    var data = result.data.rows;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        html += "<tr >\n" +
                            "\t\t\t                              <td><input name='check' value='" + data[i].id + "' type=\"checkbox\" ></td>\n" +
                            "\t\t\t\t                          <td>" + data[i].id + "</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + data[i].specName + "</td>\n" +
                            "\t\t                                  <td class=\"text-center\">\n" +
                            "\t\t                                 \t  <button type=\"button\" class=\"btn bg-olive btn-xs\" onclick='UpdateSpec(" + data[i].id + ")' data-toggle=\"modal\" data-target=\"#UpdateeditModal\">修改</button>\n" +
                            "\t\t                                  </td>\n" +
                            "\t\t\t                          </tr>";
                        $("#HtmlId").html(html)
                    }
                    $("#pageNum").val(result.data.pageNum)
                    $("#pages").val(result.data.pages)
                    $("#total").val(result.data.total)
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理查询失败")
            }
        })
    }

    function findSpecList() {
        var FormId = $("#FormId").serializeObject();//serializeObject 是自定义文件中自定义的一个方法
        $.ajax({
            url: domain + "/spec/findSpecList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(FormId),//往后台发送的确确实实是一个json数据 JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    var data = result.data;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        html += "<tr >\n" +
                            "\t\t\t                              <td><input name='check' value='" + data[i].id + "' type=\"checkbox\" ></td>\n" +
                            "\t\t\t\t                          <td>" + data[i].id + "</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + data[i].specName + "</td>\n" +
                            "\t\t                                  <td class=\"text-center\">\n" +
                            "\t\t                                 \t  <button type=\"button\" class=\"btn bg-olive btn-xs\" onclick='UpdateSpec(" + data[i].id + ")' data-toggle=\"modal\" data-target=\"#UpdateeditModal\">修改</button>\n" +
                            "\t\t                                  </td>\n" +
                            "\t\t\t                          </tr>";
                        $("#HtmlId").html(html)
                    }
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }

    /*分页*/
    function toPage(id) {
        var pageNum = parseInt($("#pageNum").val());
        var pages = parseInt($("#pages").val());
        if (id == 1) {
            if (pageNum <= 1) {
                alert("已是首页");
                return;
            }
            $("#pageNum").val(pageNum - 1);
        } else if (id == 2) {
            if (pageNum >= pages) {
                alert("已是尾页");
                return;
            }
            $("#pageNum").val(pageNum + 1);
        } else if (id == 3) {
            $("#pageNum").val(1);
        } else if (id == 4) {
            $("#pageNum").val(pages);
        }
        getSpecList();
    }

    //回显
    function UpdateSpec(id) {
        $.ajax({
            url: domain + "/spec/getSpecById",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            // contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "get",//必须指定请求类型
            data: {id: id},//往后台发送的确确实实是一个json数据     JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    $("#specName").val(result.data.specName)
                    $("#id1").val(result.data.id)
                    var data = result.data.specOptionVOList;
                    var spec = "";
                    for (let i = 0; i < data.length; i++) {
                        spec += "<tr>\n" +
                            "                        <td>\n" +
                            "                            <input value='" + data[i].optionName + "' class=\"form-control\" placeholder=\"规格选项\">\n" +
                            "                        </td>\n" +
                            "                        <td>\n" +
                            "                            <input value='" + data[i].orders + "' class=\"form-control\" placeholder=\"排序\">\n" +
                            "                        </td>\n" +
                            "                        <td>\n" +
                            "                            <button type=\"button\" class=\"btn btn-default\" onclick='delTr(this)' title=\"删除\"><i class=\"fa fa-trash-o\"></i> 删除\n" +
                            "                            </button>\n" +
                            "                        </td>\n" +
                            "                    </tr>";
                    }
                    $("#spec_update_tbody").html(spec);
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }


    //自增表格
    $("#addspecOption").click(function () {
        var tr = "<tr>\n"
        tr += "\n"
        tr += "                        <td>\n"
        tr += "                            <input class=\"form-control\" placeholder=\"规格选项\">\n"
        tr += "                        </td>\n"
        tr += "                        <td>\n"
        tr += "                            <input class=\"form-control\" placeholder=\"排序\">\n"
        tr += "                        </td>\n"
        tr += "                        <td>\n"
        tr += "                            <button type=\"button\" class=\"btn btn-default\" onclick='delTr(this)' title=\"删除\"><i class=\"fa fa-trash-o\"></i> 删除\n"
        tr += "                            </button>\n"
        tr += "                        </td>\n"
        tr += "                    </tr>";
        $("#spec_add_tbody").append(tr);/*追加一行，问题：格式不同*/
    });

    /*button的id*/
    $("#updatespecOption").click(function () {
        var tr = "<tr>\n"
        tr += "\n"
        tr += "                        <td>\n"
        tr += "                            <input class=\"form-control\" placeholder=\"规格选项\">\n"
        tr += "                        </td>\n"
        tr += "                        <td>\n"
        tr += "                            <input class=\"form-control\" placeholder=\"排序\">\n"
        tr += "                        </td>\n"
        tr += "                        <td>\n"
        tr += "                            <button type=\"button\" class=\"btn btn-default\" onclick='delTr(this)' title=\"删除\"><i class=\"fa fa-trash-o\"></i> 删除\n"
        tr += "                            </button>\n"
        tr += "                        </td>\n"
        tr += "                    </tr>";
        $("#spec_update_tbody").append(tr);/*追加一行，问题：格式不同*/
        /*tbody的id*/
    });

    function delTr(nowTr) {
        /*
        * button的父级是td，td的父级是tr所以parent.parent
        * 删除当前行
        * 多一个parent就代表向前一个标签
        * 如果多一个parent就会删除整个table
        * */
        $(nowTr).parent().parent().remove()
    }


    //修改
    function Update() {
        var specVo = {};
        //{"id":26，"specName":尺
        var specid = $("#id1").val();
        var specname = $("#specName").val();
        specVo["id"] = specid;
        specVo["specName"] = specname;
        console.log("1--")
        console.log(specVo)
        //拼接规格选项的集合
        var list = [];
        var trList = $("#spec_update_tbody").find("tr");//遍历
        $(trList).each(function (i, e) {
            var temp = {};
            var tdList = $(e).find("td");
            //获取每一列
            temp["specId"] = $("#id1").val();
            temp["optionName"] = $(tdList[0]).find("input").val();
            temp["orders"] = $(tdList[1]).find("input").val();//存入List集合中
            list.push(temp);
        })
        specVo["specOptionVOList"] = list;
        console.log("2--")
        console.log(specVo)
        $.ajax({
            url: domain + "/spec/updateSecp",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "put",//必须指定请求类型
            data: JSON.stringify(specVo),//往后台发送的确确实实是一个json数据   JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    alert("修改成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }


    function InsertSpec() {
        var specVo = {};
        var specname = $('input[name="InsertspecName"]').val();
        specVo["specName"] = specname;
        console.log("3--")
        console.log(specVo)
        //拼接规格选项的集合
        var list = [];
        var trList = $("#spec_add_tbody").find("tr");//遍历
        $(trList).each(function (i, e) {
            var temp = {};
            var tdList = $(e).find("td");
            //获取每一列
            temp["optionName"] = $(tdList[0]).find("input").val();
            temp["orders"] = $(tdList[1]).find("input").val();//存入List集合中
            list.push(temp);
        })
        specVo["specOptionVOList"] = list;
        console.log("4--")
        console.log(specVo)
        $.ajax({
            url: domain + "/spec/InsertSpec",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(specVo),//往后台发送的确确实实是一个json数据   JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    alert("增加成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }

    //批量删除
    function DelteSpec() {
        var arr = [];
        var check = document.getElementsByName("check");
        for (let i = 0; i < check.length; i++) {
            if (check[i].checked) {
                arr.push(check[i].value)
            }
        }
        console.log(arr)
        if (arr >= 1) {
            console.log(arr)
        if (confirm("确认要删除吗？")) {
            $.ajax({
                url: domain + "/spec/deleteSpec/" + arr,
                headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
                dcontentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
                dataType: "json",
                type: "delete",//必须指定请求类型
                // data:{arr:arr},//往后台发送的确确实实是一个json数据
                success: function (result) {
                    if (result.code == 11) {
                        alert("删除成功")
                    } else if (result.code == 12) {
                        top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                    }
                }
            })
        }
        }else{
            alert("请选择要删除的数据")
        }
    }
</script>
</html>